package com.yantong.controller.centre;

import com.yantong.controller.BaseController;
import com.yantong.utils.DateUtils;
import com.yantong.utils.PageUtils;
import com.yantong.utils.excel.ExcelHelper;
import com.yantong.pojo.Member;
import com.yantong.service.RegCommanderService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
  * @Description:
  * Copyright: Copyright (c) 2018 
  * Company: 已关闭门店
  * @author xiangyi
  * @version V1.0.0
  * @since 3.0
  * @date 2018年9月3日 上午10:48:23
 */
@Controller
@RequestMapping("/closeCommander/manage")
public class RegCloseCommanderController extends BaseController {
    
    private static final Logger logger = LoggerFactory.getLogger(RegCloseCommanderController.class);

    private static final String prefix = "commander/manage";

    @Autowired
    private RegCommanderService regCommanderService;

    /**
     * 已关闭门店列表界面
     * @return
     */
    @GetMapping()
    @RequiresPermissions("closeCommander:manage:manage")
    public String closeCommanders(Model model) {
        model.addAllAttributes(getCentreIdModelInfo());
        return prefix + "/closeCommanders";
    }


    /**
     * 已关闭门店列表数据
     * @param member
     * @param offset
     * @param limit
     * @return
     */
    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("closeCommander:manage:manage")
    public PageUtils<Member> listCloseCommander(Member member, int offset, int limit){
        if (member.getCentreId() == null) {
            member.setCentreId(getUserCentreId());
        }
        return regCommanderService.listCloseCommander(member, offset, limit);
    }
    
    /**
     * 已关闭门店-导出
     * @param response
     * @param member
     */
    @RequiresPermissions("closeCommander:manage:export")
    @GetMapping("/exportCloseCommander")
    public void exportCloseCommander(HttpServletResponse response, Member member) {
        List<List<Object>> resultData = new ArrayList<>();
        String[] head = {"用户名[ID]","性别","手机号码","门店推荐人","实名认证","累计余额","申请时间","关闭时间","关闭原因"};
        resultData.add(0, Arrays.asList(head));
        if(member.getCentreId() == null) {
            member.setCentreId(getUserCentreId());
        }
        List<Member> list = regCommanderService.listCloseCommander(member);
        if (CollectionUtils.isNotEmpty(list)){
            for(Member mem: list) {
                List<Object> row = new ArrayList<>();
                String name = "";
                if(StringUtils.isNotEmpty(mem.getRealname())){
                    name = mem.getRealname()+"["+mem.getId()+"]";
                }else{
                    if(StringUtils.isNotEmpty(mem.getNickname())){
                        name = mem.getNickname()+"["+mem.getId()+"]";
                    }else{
                        name = "["+mem.getId()+"]";
                    }
                }
                row.add(name);
                String gender = "";
                if(mem.getGender()!=null){
                    if(mem.getGender()==1) gender = "男";
                    if(mem.getGender()==2) gender = "女";
                }
                row.add(gender);
                row.add(mem.getMobile()!=null?mem.getMobile():"");
                row.add(mem.getUpTeamname()!=null?mem.getUpTeamname():"");
                String smz = "";
                if(mem.getShimingzhi()!=null){
                    if(mem.getShimingzhi()==1) smz = "已认证";
                    if(mem.getShimingzhi()==0) smz = "未认证";
                }
                row.add(smz);
                row.add(mem.getCredit2()!=null?mem.getCredit2():"");
                row.add(mem.getsTime()!=null?DateUtils.format(mem.getsTime(),"yyyy-MM-dd HH:mm:ss"):"");
                row.add(mem.getCloseTime()!=null?DateUtils.format(mem.getCloseTime(),"yyyy-MM-dd HH:mm:ss"):"");
                row.add(mem.getCloseReason()!=null?mem.getCloseReason():"");
                resultData.add(row);
            }
            String fileName = "已关闭门店列表_" + DateUtils.format(new Date(), "yyyyMMddHHmmss");
            try {
                ExcelHelper.exportExcel(response, fileName, "已关闭门店列表", resultData);
            } catch (IOException e) {
                logger.info("export member error:{}", e.getMessage());
            }
        }
    }
    
}
